<?php

namespace app\essayhandle\controller;


//该类用户文章,视频的处理,分类，打标签等
use think\Db;
use think\Exception;

class Item
{

    private $num = 0;

    //文章处理
    public function NewsHandle()
    {
        try {

            //从mysql 读取数据
            $news_test_obj = Db::name('news_test');

            $list = $news_test_obj->where(['is_read' => 0, 'is_success' => 0])->limit('15')->select();

            $list_id = [];
            foreach ($list as $value) {
                $list_id[] = $value['id'];
            }

            //更新已读状态
            $result = $news_test_obj->where('id', 'in', $list_id)->update(['is_read' => 1]);

            if (!$result) {
                throw new Exception('更新is_read状态失败');
            }

            //分类等
            foreach ($list as $value) {
                $title = $value['title'];

                $content = $value['content'];

                $id = $value['id'];

//                //文本审核
//                $contentAipTextCheckResult = $this->aipTextCheck($content, '内容', $id);
//
//                if (!$contentAipTextCheckResult) {
//                    continue;
//                }

                //智能分类结果
                $navigation_name = $this->getArticleTopic($title, $content, $id);

                if (!$navigation_name) {
                    continue;
                }

                //智能标签结果
                $tags = $this->getArticleKeyword($title, $content, $id);

                $tag_arr = [];
                $tags_airec = '';
                foreach ($tags as $value) {
                    $tag_arr[] = $value['tag'];
                    $tags_airec = $tags_airec . $value['tag'] . ',';
                }

                $obj = new Addessay();

                $result = $obj->addEssayMongo($title, $content, $navigation_name, $tags_airec, $tag_arr);


                if ($result) {
                    $news_test_obj->where(['id' => $id])->update(['is_read' => 1, 'is_success' => 1]);
                    echo "处理成功(上传mongo)\r\n";
                } else {
                    $news_test_obj->where(['id' => $id])->update(['is_read' => 1, 'is_success' => 2]);
                    echo "上传mongo失败\r\n";
                }
            }
        } catch
        (\Exception $exception) {
            errorBack($exception);
        }
    }

    //获取文章的标签
    private function getArticleKeyword($title, $content, $news_test_id)
    {
        echo "文章打标签开始\r\n";
        //请求的url
        $url = 'http://mp.feiyanweb.com/api/article_interface/getArticleKeyword';

        //请求的参数
        $data = ['title' => $title, 'content' => $content];

        //审核标题,文本,判断标题，文本是否合规,不合规则中断本次的执行
        $articleKeyWordResult = curl_post($url, $data);

        $news_test_obj = Db::name('news_test');

        if ($articleKeyWordResult && $articleKeyWordResult['code'] == 200) {

            $tags = json_decode($articleKeyWordResult['msg'], true)['items'];

            return $tags;

        } else {
            echo "文章 标签 请求失败,审核下一条数据\r\n";
            $news_test_obj->where(['id' => $news_test_id])->update(['is_read' => 1, 'is_success' => 2]);
            return false;
        }
    }

    //获取文章分类
    private function getArticleTopic($title, $content, $news_test_id)
    {

        echo "文章分类开始\r\n";

        //请求的url
        $url = 'http://mp.feiyanweb.com/api/article_interface/getArticleTopic';

        //请求的参数
        $data = ['title' => $title, 'content' => $content];

        //审核标题,文本,判断标题，文本是否合规,不合规则中断本次的执行
        $articleTopicResult = curl_post($url, $data);


        $news_test_obj = Db::name('news_test');

        if ($articleTopicResult && $articleTopicResult['code'] == 200) {

            $articleTopicResultMsg = json_decode($articleTopicResult['msg'], true);

            //分类名
            $navigation_name = $articleTopicResultMsg['item']['lv1_tag_list'][0]['tag'];

            $result = $this->insertNavigationName($navigation_name, 1);

            if ($result) {
                return $navigation_name;
            } else {
                $news_test_obj->where(['id' => $news_test_id])->update(['is_read' => 1, 'is_success' => 2]);
                return false;
            }
        } else {

            echo "文章 分类 请求失败,审核下一条数据\r\n";
            $news_test_obj->where(['id' => $news_test_id])->update(['is_read' => 1, 'is_success' => 2]);
            return false;
        }

    }

    //0:未处理
    //1:处理成功
    //2:处理失败(请求失败)
    //3:不合规

    //文本审核($content表示文本的内容),$type类型(审核标题/还是文章内容),文章的id，用户表的对象
    private function aipTextCheck($content, $type, $news_test_id)
    {
        //请求的url
        $url = 'http://mp.feiyanweb.com/api/article_interface/aipTextCheck';

        //请求的参数
        $data = ['text' => $content];


        //审核标题,文本,判断标题，文本是否合规,不合规则中断本次的执行
        $apiTextCheckResult = curl_post($url, $data);

        $news_test_obj = Db::name('news_test');

        if ($apiTextCheckResult && $apiTextCheckResult['code'] == 200) {
            echo "文章 $type 审核请求成功\r\n";

            //判断当前文章是不是合规
            $apiTextCheckResultMsg = json_decode($apiTextCheckResult['msg'], true);

            if ($apiTextCheckResultMsg['conclusionType'] == 1) {
                echo "文章 $type 审核结果:文章 $type 合规,进入图片审核(如果图片存在)\r\n";
                return true;
            } else {
                $error_msg = $apiTextCheckResultMsg['data'][0]['msg'];
                echo "文章 $type 审核结果:文章 $type 不合规:$error_msg -> 进入下一条文章审核\r\n";
                $news_test_obj->where(['id' => $news_test_id])->update(['is_read' => 0, 'is_success' => 3, 'msg' => $error_msg]);
                return false;
            }

        } else {
            echo "文章 $type 审核请求失败,审核下一条数据\r\n";
            $news_test_obj->where(['id' => $news_test_id])->update(['is_read' => 0, 'is_success' => 2]);
            return false;
        }
    }

    //读取excel数据,并写到mysql中
    public function getExcleData()
    {
        try {
        $objPHPExcel = \PHPExcel_IOFactory::load('/code/web/company/qizhi/feiyan/test.csv');

        //获取sheet表格数目

        //默认选中sheet0表
        $sheetSelected = 0;
        $objPHPExcel->setActiveSheetIndex($sheetSelected);

        $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow();

        $addData = [];

        for ($row = 2; $row <= $rowCount; $row++) {

            $title = $objPHPExcel->getActiveSheet()->getCell('A' . $row)->getValue();

            $content = $objPHPExcel->getActiveSheet()->getCell('M' . $row)->getValue();

            if ($title && $content) {
                $arr = [];

                $arr['title'] = $title;

                $arr['content'] = $content;

                $addData[] = $arr;
            }
        }

        var_dump($addData);

        //print_r($addData) ;

        //$result = Db::name('news_test')->insertAll($addData);

//            if ($result) {
//                successBack([],'插入数据成功');
//            }else{
//                throw new Exception('插入数据失败');
//            }
        } catch (\Exception $exception) {
            errorBack($exception);
        }
    }

    //判断分类名是不是存在,不存在则插入$navigation_name导航(分类)名,$type 1:首页资讯分类,2:视频模块
    public function insertNavigationName($navigation_name, $type)
    {
        switch ($type) {
            case 1:
                echo '首页资讯';
                break;
            case 2:
                echo '视频模块';
                break;
        }

        $obj = Db::connect("db_mongo")->name('item_navigation');

        //判断当前导航名是不是存在
        $result = $obj->where(['navigation_name' => $navigation_name])->field('navigation_name')->find();

        if ($result) {
            return true;
        } else {
            //插入新的导航
            $addData = [];

            $addData['navigation_name'] = $navigation_name;
            $addData['navigation_status'] = 1;
            $addData['navigation_type'] = $type;

            $result = $obj->order(['navigation_weight' => 1])->find();


            $addData['navigation_weight'] = $result['navigation_weight'] - 1;

            $addData['scene_id'] = 'a' . getMid('scene_id');

            $result = $obj->insertGetId($addData);

            if ($result) {
                return true;
            } else {
                return false;
            }
        }
    }
}